﻿using NeuroCheck.PlugInLib.API;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ControlEdit
{
    public class DataGridViewEdit
    {
        /// <summary>
        /// 设置dataGridView参数显示
        /// </summary>
        /// <param name="parName">参数名称</param>
        /// <param name="parValue">参数值</param>
        /// <param name="dataGridView"></param>
        public static void DataGridViewSet_Cfg(string parName, string[] parValue, string parType, DataGridView dataGridView)
        {
            //数据名称
            //创建行
            DataGridViewRow dataGridViewRow = new DataGridViewRow();

            //创建TextBox
            DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
            dataGridViewTextBoxCell.Value = parName;
            dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);

            //值只有一个，显示textbox，多个显示下拉框
            if (parValue.Length == 1)
            {
                DataGridViewTextBoxCell dataGridViewTextBoxCell_Value = new DataGridViewTextBoxCell();
                dataGridViewTextBoxCell_Value.Value = parValue[0];
                dataGridViewRow.Cells.Add(dataGridViewTextBoxCell_Value);
            }
            else if (parValue.Length > 1)  //多个显示下拉框
            {
                DataGridViewComboBoxCell dataGridViewComboBoxCell = new DataGridViewComboBoxCell();
                for (int i = 0; i < parValue.Length; i++)
                {
                    dataGridViewComboBoxCell.Items.Add(parValue[i]);
                }
                dataGridViewComboBoxCell.Value = parValue[0];
                dataGridViewRow.Cells.Add(dataGridViewComboBoxCell);
            }

            //数据类型
            //创建TextBox
            DataGridViewTextBoxCell dataGridViewTextBoxCell1 = new DataGridViewTextBoxCell();
            dataGridViewTextBoxCell1.Value = parType;
            dataGridViewRow.Cells.Add(dataGridViewTextBoxCell1);

            dataGridView.Rows.Add(dataGridViewRow);
        }

        /// <summary>
        /// 参数显示在dataGridView
        /// </summary>
        /// <param name="parName">参数名称</param>
        /// <param name="par_TrayOrRegister">Tray、Register,显示的下拉数据</param>
        /// <param name="TrayOrRegister">存储在Tray还是Register</param>
        /// <param name="parValue">参数值</param>
        /// <param name="dataGridView">显示在哪个DataGridView</param>
        public static void DataGridViewSet(string parName, string[] par_TrayOrRegister, string TrayOrRegister, string parValue, DataGridView dataGridView)
        {
            //数据名称
            //创建行
            DataGridViewRow dataGridViewRow = new DataGridViewRow();

            //创建TextBox
            DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
            dataGridViewTextBoxCell.Value = parName;
            dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);

            //值只有一个，显示textbox，多个显示下拉框
            if (par_TrayOrRegister.Length == 1)
            {
                DataGridViewTextBoxCell dataGridViewTextBoxCell_Value = new DataGridViewTextBoxCell();
                dataGridViewTextBoxCell_Value.Value = TrayOrRegister;
                dataGridViewRow.Cells.Add(dataGridViewTextBoxCell_Value);
            }
            else if (par_TrayOrRegister.Length > 1)  //多个显示下拉框
            {
                DataGridViewComboBoxCell dataGridViewComboBoxCell = new DataGridViewComboBoxCell();
                for (int i = 0; i < par_TrayOrRegister.Length; i++)
                {
                    dataGridViewComboBoxCell.Items.Add(par_TrayOrRegister[i]);
                }
                dataGridViewComboBoxCell.Value = TrayOrRegister;
                dataGridViewRow.Cells.Add(dataGridViewComboBoxCell);
            }

            if (TrayOrRegister == "Register")
            {
                //获取寄存器数据
                int[] inputRegisterIds = NcPIDataRegisterManager.GetInputRegisterIDs();
                DataGridViewComboBoxCell dataGridViewComboBoxCell = new DataGridViewComboBoxCell();
                for (int i = 0; i < inputRegisterIds.Length; i++)
                {
                    //获取寄存器数据格式
                    var properties = NcPIDataRegisterManager.GetRegisterProperties(inputRegisterIds[i]);
                    NcPIRegisterDataTypeInfo propertyInfo = properties.DataType;  //寄存器存储数据类型
                    NcPIDataRegisterManager.ReadFromRegister(inputRegisterIds[i], out object obj);   //寄存器存储的值
                    dataGridViewComboBoxCell.Items.Add(inputRegisterIds[i].ToString() + ":" + propertyInfo.ToString());
                }
            
                dataGridViewComboBoxCell.Value = parValue;
                dataGridViewRow.Cells.Add(dataGridViewComboBoxCell);
            }
            else if (TrayOrRegister == "Tray")
            {
                //创建TextBox
                DataGridViewTextBoxCell dataGridViewTextBoxCell1 = new DataGridViewTextBoxCell();
                dataGridViewTextBoxCell1.Value = parValue;
                dataGridViewRow.Cells.Add(dataGridViewTextBoxCell1);
            }

            dataGridView.Rows.Add(dataGridViewRow);
        }

        /// <summary>
        /// DataGridView数据转成DataTable
        /// </summary>
        /// <param name="dgv"></param>
        /// <returns></returns>
        public static DataTable DataGridViewToTable(DataGridView dgv)
        {
            DataTable dt = new System.Data.DataTable();

            // 循环列
            for (int count = 0; count < dgv.Columns.Count; count++)
            {
                dt.Columns.Add(dgv.Columns[count].HeaderText.ToString());
            }
            DataRow dr;
            // 循环行
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                dr = dt.NewRow();
                for (int countsub = 0; countsub < dgv.Columns.Count; countsub++)
                {
                    dr[countsub] = Convert.ToString(dgv.Rows[i].Cells[countsub].Value);
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

        /// <summary>
        /// DataGridView转成List
        /// </summary>
        /// <param name="dgv">DataGridView</param>
        /// <param name="ErrorText">错误信息</param>
        /// <returns></returns>
        public static List<(string, string, string)> DataGridViewToList(DataGridView dgv, out string ErrorText)
        {
            List<(string, string, string)> tempList = new List<(string, string, string)>();
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                (string, string, string) tempTuple;
                //第一列：参数名称
                tempTuple.Item1 = dgv.Rows[i].Cells[0].Value.ToString();

                //第二列：存储位置
                if (!string.IsNullOrEmpty(dgv.Rows[i].Cells[1].Value?.ToString()))
                {
                    tempTuple.Item2 = dgv.Rows[i].Cells[1].Value.ToString();
                }
                else
                {
                    ErrorText = dgv.Rows[i].Cells[0].Value.ToString() + "的存储位置尚未设置";
                    return null;
                }

                //第三列：索引号
                if (!string.IsNullOrEmpty(dgv.Rows[i].Cells[2].Value?.ToString()))
                {

                    tempTuple.Item3 = dgv.Rows[i].Cells[2].Value.ToString();
                }
                else
                {
                    ErrorText = dgv.Rows[i].Cells[0].Value.ToString() + "的存储位置索引号尚未设置";
                    return null;
                }

                tempList.Add(tempTuple);
            }
            ErrorText = "";
            return tempList;
        }

        /// <summary>
        /// DataGridView转成List
        /// </summary>
        /// <param name="dgv"></param>
        /// <returns></returns>
        public static List<(string, string, string)> DataGridViewToList(DataGridView dgv)
        {
            List<(string, string, string)> tempList = new List<(string, string, string)>();
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                (string, string, string) tempTuple;

                tempTuple.Item1 = dgv.Rows[i].Cells[0].Value.ToString();

                if (!string.IsNullOrEmpty(dgv.Rows[i].Cells[1].Value?.ToString()))
                {
                    tempTuple.Item2 = dgv.Rows[i].Cells[1].Value.ToString();
                }
                else
                {
                    tempTuple.Item2 = "";
                }

                if (!string.IsNullOrEmpty(dgv.Rows[i].Cells[2].Value?.ToString()))
                {
                    tempTuple.Item3 = dgv.Rows[i].Cells[2].Value.ToString();
                }
                else
                {
                    tempTuple.Item3 = "";
                }
                tempList.Add(tempTuple);
            }

            return tempList;
        }
        /// <summary>
        /// 存储cfg参数的DataGridView转成List
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="ErrorText"></param>
        /// <returns></returns>
        public static List<(string, string, List<string>, string)> DataGridViewToList_cfg(DataGridView dgv, out string ErrorText)
        {
            List<(string, string, List<string>, string)> tempList = new List<(string, string, List<string>, string)>();
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                (string, string, List<string>, string) tempTuple;

                //参数名称
                tempTuple.Item1 = dgv.Rows[i].Cells[0].Value?.ToString();

                //参数值-非下拉框
                if (!string.IsNullOrEmpty(dgv.Rows[i].Cells[1].Value?.ToString()))
                {
                    tempTuple.Item2 = dgv.Rows[i].Cells[1].Value.ToString();
                }
                else
                {
                    ErrorText = dgv.Rows[i].Cells[0].Value.ToString() + "的参数值尚未设置";
                    return null;
                }

                //下拉框值
                List<string> tempLs = new List<string>();
                //判断是否为下拉框
                if (dgv.Rows[i].Cells[1].GetType() == typeof(DataGridViewComboBoxCell))  //下拉框
                {
                    DataGridViewComboBoxCell tempCell = dgv.Rows[i].Cells[1] as DataGridViewComboBoxCell;
                    for (int j = 0; j < tempCell.Items.Count; j++)
                    {
                        tempLs.Add(tempCell.Items[j].ToString());
                    }
                }
                else   //不是下拉框
                {
                    string str = dgv.Rows[i].Cells[1].Value?.ToString();
                    tempLs.Add(str);
                }
                tempTuple.Item3 = tempLs;

                //存储数据类型
                tempTuple.Item4 = dgv.Rows[i].Cells[2].Value?.ToString();



                tempList.Add(tempTuple);
            }
            ErrorText = "";
            return tempList;

        }

        /// <summary>
        /// 存储cfg参数的DataGridView转成List
        /// </summary>
        /// <param name="dgv"></param>
        /// <returns></returns>
        public static List<(string, string, List<string>, string)> DataGridViewToList_cfg(DataGridView dgv)
        {
            List<(string, string, List<string>, string)> tempList = new List<(string, string, List<string>, string)>();
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                (string, string, List<string>, string) tempTuple;

                tempTuple.Item1 = dgv.Rows[i].Cells[0].Value.ToString();
                if (!string.IsNullOrEmpty(dgv.Rows[i].Cells[1].Value?.ToString()))
                {
                    tempTuple.Item2 = dgv.Rows[i].Cells[1].Value.ToString();
                }
                else
                {
                    tempTuple.Item2 = "";
                }

                //用来存储下拉框值
                List<string> tempLs = new List<string>();
                //判断是否为下拉框
                if (dgv.Rows[i].Cells[1].GetType() == typeof(DataGridViewComboBoxCell))
                {
                    DataGridViewComboBoxCell tempCell = dgv.Rows[i].Cells[1] as DataGridViewComboBoxCell;
                    for (int j = 0; j < tempCell.Items.Count; j++)
                    {
                        tempLs.Add(tempCell.Items[j].ToString());
                    }
                }
                else
                {
                    string str = dgv.Rows[i].Cells[1].Value?.ToString();
                    tempLs.Add(str);
                }
                tempTuple.Item3 = tempLs;

                //存储数据类型
                tempTuple.Item4 = dgv.Rows[i].Cells[2].Value?.ToString();

                tempList.Add(tempTuple);
            }
            return tempList;

        }

        /// <summary>
        ///判断当前的数据是否超限
        /// </summary>
        /// <param name="dgv">图标参数，控制参数显示的表格</param>
        /// <param name="ErrorText">错误信息</param>
        /// <returns></returns>
        public static bool ValueLimit(DataGridView dgv, out string ErrorText)
        {
            ErrorText = "";
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                //判断输入的索引号值
                string Index = dgv.Rows[i].Cells[2].Value.ToString();
                Index = Index.Contains(":") ? Index.Split(':')[0] : Index;
                //判断能否转换为int
                bool isInt = int.TryParse(Index, out int value);
                if (!isInt)
                {
                    ErrorText = dgv.Rows[i].Cells[0].Value.ToString() +"输入的值有误";
                    return false;
                }

                //判断是Tray还是Register
                string TrayOrRegister = dgv.Rows[i].Cells[1].Value.ToString();
                if (TrayOrRegister == "Tray")
                {
                    if (0 <= value && value <= 999)
                    {

                    }
                    else
                    {
                        ErrorText = dgv.Rows[i].Cells[0].Value.ToString()+ "输入的值不在Tray的ID范围内";
                        return false;
                    }
                }
                else if (TrayOrRegister == "Register")
                {
                    if (0 <= value && value <= 999999)
                    {

                    }
                    else
                    {
                        ErrorText = dgv.Rows[i].Cells[0].Value.ToString()+ "输入的值不在Register的ID范围内";
                        return false;
                    }
                }

            }
            return true;
        }
    }
}

